package gre.core;

import gre.db.*;
import java.sql.*;
import java.util.*;

/**
 *
 * @author nacuong
 */
public class TestGenerator {

  /**
   * Randomly select n words from words table
   * @param n
   * @return a vector of n wordIds from words table
   *
   * //TODO generate words that lessly being displayed
   * //TODO generate multiple choices test
   */
  public static Vector<Integer> generateWordIds(Statement stmt, int n) throws SQLException{
    Vector<Integer> words = new Vector<Integer>();
    /* retrieve only minimum of n or table size words */
    int tableSize = DBHelper.getTableSize(stmt, "words");
    n = Math.min(tableSize, n);

    /* generate a list of random wordid */
    while (words.size() < n) {
      Random random = new Random();
      int wordId = random.nextInt(tableSize) + 1;
      if (!words.contains(wordId))
        words.add(wordId);
    }

    return words;
  }

  /**
   * Generat a word list from the given word id list
   * @param stmt
   * @param wordIds
   * @return
   * @throws SQLException
   */
  public static Vector<String> generateWords(Statement stmt, Vector<Integer> wordIds) throws SQLException{
    Vector<String> words = new Vector<String>();
    for (int wordId : wordIds) {
      String word = DBSelector.selectWord(stmt, wordId);
      words.add(word);
    }

    return words;
  }

  /**
   * Generate a list of sentences (including the meaning) from the given word it list.
   * The size of sentence list is only a half of the word if list size.
   *
   * @param stmt
   * @param wordIds
   * @return
   * @throws SQLException
   */
  public static Vector<String[]> generateSentences(Statement stmt, Vector<Integer> wordIds) throws SQLException {
    Vector<String[]> sentencePairs = new Vector<String[]>();

    /* accumulate all sentences from word list */
    for (int wordId : wordIds) {
      String word = DBSelector.selectWord(stmt, wordId);
      String meaning = DBSelector.selectMeaning(stmt, wordId);
      Vector<String> sentences = DBSelector.selectStatement(stmt, wordId);
      String[] pairWM = {word, meaning};
      sentencePairs.add(pairWM);
      
      for(String sentence: sentences){
        sentence = sentence.replaceAll(word, "___________");
        String[] pairWS = {word, sentence};
        sentencePairs.add(pairWS);
      }
    }

    /* choose only a half of number of sentences */
    int size = sentencePairs.size();
    Random random = new Random();
    Vector<String[]> qaPairs = new Vector<String[]>();
    while (sentencePairs.size() != (int)(size/2)) {
      int sIndex = random.nextInt(sentencePairs.size());
      qaPairs.add(sentencePairs.get(sIndex));
      sentencePairs.remove(sIndex);
    }

    return qaPairs;
  }

  /**
   * Generate answers from the pair of sentence list
   * @param sentences
   * @return
   */
  public static Vector<String> generateAnswers(Vector<String[]> sentences){
    Vector<String> answers = new Vector<String>();

    for (String[] sentence: sentences)
      answers.add(sentence[0]);

    return answers;
  }

  /**
   * Generate questions from the pair of sentence list
   * @param sentences
   * @return
   */
  public static Vector<String> generateQuestions(Vector<String[]> sentences){
    Vector<String> questions = new Vector<String>();

    for (String[] sentence: sentences)
      questions.add(sentence[1]);

    return questions;
  }
}
